#include "network-panel-wan.h"

// 获取连接状态, ip地址，掩码，网关
bool GetConnectStatus(char *dev) {
    if(NetworkConfigExists(GATEWAY_WIRED_WAN_CONFIG) && NetworkConfigInterfaceExists(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE)) {
        
    }
    return false;
}

bool WiredSetWork(ConfigInterface *interface, char *workType) {
    strcpy(interface->workType,NET_IFACE_ATTR_WORK_DHCP);
    return true;
}

bool WiredSetDynamic() {
    ConfigInterface interface = {0};
    if(NetworkConfigExists(GATEWAY_WIRED_WAN_CONFIG) && NetworkConfigInterfaceExists(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE)) {
        // 读取出该接口所有属性，由NetworkConfig.*提供
        if (NetworkConfigInterface(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface)) {
            INFO_LOG("iface %s %s %s: %s", interface.iface,interface.ipType,interface.workType,interface.isAuto?"isAuto":"notAuto");
            if (NetworkConfigInterfaceSectionProperties(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface.properties)) {
                WiredSetWork(&interface, NET_IFACE_ATTR_WORK_DHCP);

                GSList *properties = interface.properties;
                if (NetworkConfigModifyConfigInterface(GATEWAY_WIRED_WAN_CONFIG, &interface)) {
                    
                }
                NC_PROPERTY_FREE_ALL_CLEAN(interface.properties)
            }
        }
    }
    return true;
}

bool WiredSetStatic() {
    ConfigInterface interface = {0};
    if(NetworkConfigExists(GATEWAY_WIRED_WAN_CONFIG) && NetworkConfigInterfaceExists(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE)) {
        // 读取出该接口所有属性，由NetworkConfig.*提供
        if (NetworkConfigInterface(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface)) {
            INFO_LOG("iface %s %s %s: %s", interface.iface,interface.ipType,interface.workType,interface.isAuto?"isAuto":"notAuto");
            if (NetworkConfigInterfaceSectionProperties(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface.properties)) {
                WiredSetWork(&interface, NET_IFACE_ATTR_WORK_STATIC);

                GSList *properties = interface.properties;
                if (NetworkConfigModifyConfigInterface(GATEWAY_WIRED_WAN_CONFIG, &interface)) {

                }
                NC_PROPERTY_FREE_ALL_CLEAN(properties)
            }
        }
    }
    return true;
}

bool WiredSetManual() {
    ConfigInterface interface = {0};
    if(NetworkConfigExists(GATEWAY_WIRED_WAN_CONFIG) && NetworkConfigInterfaceExists(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE)) {
        // 读取出该接口所有属性，由NetworkConfig.*提供
        if (NetworkConfigInterface(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface)) {
            // INFO_LOG("iface %s %s %s: %s", interface.iface,interface.ipType,interface.workType,interface.isAuto?"isAuto":"notAuto");
            if (NetworkConfigInterfaceSectionProperties(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface.properties)) {
                WiredSetWork(&interface, NET_IFACE_ATTR_WORK_MANUAL);
                
                GSList *properties = interface.properties;
                if (NetworkConfigModifyConfigInterface(GATEWAY_WIRED_WAN_CONFIG, &interface)) {
                    
                }
                NC_PROPERTY_FREE_ALL_CLEAN(properties)
            }
        }
    }
    return true;
}

static bool WiredConfig(ConfigInterface *interface, char *propKey, char *propValue) {
    bool hasAddress = false;

    for (GSList *iter = interface->properties; iter; iter = iter->next) {
        // DEBUG_LOG("properties: %s -> %s", ((ConfigInterfaceProperty*)iter->data)->key, ((ConfigInterfaceProperty*)iter->data)->value);
        if (LineEqual(propKey, ((ConfigInterfaceProperty*)iter->data)->key)) {
            WARNING_LOG("属性值替换: %s -> %s", ((ConfigInterfaceProperty*)iter->data)->value, propValue);
            strcpy(((ConfigInterfaceProperty*)iter->data)->value, propValue);
            hasAddress = true;
        }
    }

    if (!hasAddress) {
        ConfigInterfaceProperty interfaceProp = {0};
        strcpy(interfaceProp.key, propKey);
        strcpy(interfaceProp.value, propValue);
        interface->properties = g_slist_append(interface->properties, createConfigInterfacePropertyCopy(&interfaceProp));
    }

    return true;
}


bool WiredSet(char *address, char *netmask, char *gateway, char *dnsaddress) {
    ConfigInterface interface = {0};
    if(NetworkConfigExists(GATEWAY_WIRED_WAN_CONFIG) && NetworkConfigInterfaceExists(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE)) {
        // 读取出该接口所有属性，由NetworkConfig.*提供
        if (NetworkConfigInterface(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface)) {
            INFO_LOG("iface %s %s %s: %s", interface.iface,interface.ipType,interface.workType,interface.isAuto?"isAuto":"notAuto");
            if (NetworkConfigInterfaceSectionProperties(GATEWAY_WIRED_WAN_CONFIG, WAN_IFACE, &interface.properties)) {
                
                if (address)
                    WiredConfig(&interface, NET_IFACE_PROP_ADDRESS, address);
                if (netmask)
                    WiredConfig(&interface, NET_IFACE_PROP_NETMASK, netmask);
                if (gateway)
                    WiredConfig(&interface, NET_IFACE_PROP_GATEWAY, gateway);
                if (dnsaddress)
                    WiredConfig(&interface, NET_IFACE_PROP_DNS, dnsaddress);

                GSList *properties = interface.properties;
                if (NetworkConfigModifyConfigInterface(GATEWAY_WIRED_WAN_CONFIG, &interface)) {
                    
                }
                NC_PROPERTY_FREE_ALL_CLEAN(properties)
            }
        }
    }
    return true;
}

// 设置IP
bool WiredSetAddress(char *address) {
    return WiredSet(address, NULL, NULL, NULL);
}
// 设置掩码
bool WiredSetNetmaskAddress(char *address) {
    return WiredSet(NULL, address, NULL, NULL);
}
// 设置网关
bool WiredSetGatewayAddress(char *address) {
    return WiredSet(NULL, NULL, address, NULL);
}
// 设置DNS
bool WiredSetDnsAddress(char *address) {
    return WiredSet(NULL, NULL, NULL, address);
}
